package com.zj.leetcode.easy.stack;

import java.util.Stack;

/**
 * @program: algorithm
 * @description: 有效的括号
 * 20 valid-parentheses
 * @author: Zhang Bo
 * @create: 2022-01-14 17:05
 **/
public class ValidParentheses {
    public static void main(String[] args) {
        String s = "{[]}";
        String[] ss = {"]", "()", "()[]{}", "(]", "([)]", "{[]}"};
        Solution solution = new ValidParentheses().new Solution();
        for (int i = 0; i < ss.length; i++) {
            System.out.println(solution.isValid(ss[i]));
        }

    }

    //leetcode submit region begin(Prohibit modification and deletion)
    class Solution {
        public boolean isValid(String s) {
            Stack<Character> stack = new Stack<>();
            for (int i = 0; i < s.length(); i++) {
                if (s.charAt(i) == '(' || s.charAt(i) == '{' || s.charAt(i) == '[') {
                    stack.push(s.charAt(i));
                } else {
                    if(stack.empty()){
                        return false;
                    }
                    if (s.charAt(i) == ')') {
                        Character pop = stack.pop();
                        if (pop != '(') {
                            return false;
                        }
                    } else if (s.charAt(i) == '}') {
                        Character pop = stack.pop();
                        if (pop != '{') {
                            return false;
                        }
                    } else if (s.charAt(i) == ']') {
                        Character pop = stack.pop();
                        if (pop != '[') {
                            return false;
                        }
                    }

                }
            }

            return stack.empty();

        }
    }
//leetcode submit region end(Prohibit modification and deletion)

}